package com.coment.message;

import com.coment.cfg.luban.fight.AttrsBuffCfg;
import com.coment.game.fight.Attrs;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.Map;

/// buff
@NoArgsConstructor
@ToString
@Getter
@Setter
public class BuffInfo {
    /// 直播间
    private Integer anchorInfoId;

    /// 技能来源角色
    public String sourceRolesId;

    /// 技能来源id
    public int sourceSkillId;

    /// 技能来源等级
    public int sourceSkillLV;

    @JsonIgnore
    /// 施法者施法技能时属性
    private Attrs sourceRolesAttrs;

    /// 作用的角色
    public String rolesId;

    /// 释放时间
    private long releaseTime;

    /// 持续时间
    private long duration;

    /// 过期时间
    private long expirationTime;

    /// 增加的属性
    public Map<Integer, Double> addValue = new HashMap<>();

    @Transient
    @JsonIgnore
    private AttrsBuffCfg cfg;

    public BuffInfo(
            int sourceSkillId,
            String sourceRolesId,
            String targetRolesId,
            long duration
    ) {
        this.sourceSkillId = sourceSkillId;
        this.sourceRolesId = sourceRolesId;
        this.rolesId = targetRolesId;
        this.releaseTime = System.currentTimeMillis();
        this.duration = duration;
        if (duration == -1L) {
            this.expirationTime = -1L;
        } else {
            this.expirationTime = this.releaseTime + duration;
        }
        this.addValue = new HashMap<>();
        this.sourceRolesAttrs = new Attrs();
    }

    public BuffInfo(
            int anchorInfoId,
            int sourceSkillId,
            String rolesId,
            String targetId,
            AttrsBuffCfg cfg
    ) {
        this.anchorInfoId = anchorInfoId;
        this.sourceSkillId = sourceSkillId;
        this.sourceRolesId = rolesId;
        this.rolesId = targetId;
        this.releaseTime = System.currentTimeMillis();
        this.duration = cfg.keep;
        if (duration == -1L) {
            this.expirationTime = -1L;
        } else {
            this.expirationTime = this.releaseTime + duration;
        }
        this.addValue = new HashMap<>();
        this.sourceRolesAttrs = new Attrs();
        this.cfg = cfg;
    }

    @JsonIgnore
    public boolean isExpired() {
        return this.expirationTime != -1L && System.currentTimeMillis() > this.expirationTime;
    }
}
